package com.uphone.kingmall.base;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.blankj.utilcode.util.KeyboardUtils;
import com.china.zhuoyue.R;
import com.uphone.kingmall.view.indicator.IndicatorAdapter;
import com.uphone.kingmall.view.indicator.TrackIndicatorView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/19
 *     desc   :
 * </pre>
 */
public abstract class BaseSearchTabActivity extends BaseActivity {
    @BindView(R.id.track_indicator)
    TrackIndicatorView trackIndicator;
    @BindView(R.id.viewPager)
    protected ViewPager viewPager;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.iv_fanhui)
    ImageView ivFanhui;
    @BindView(R.id.iv_sousuo)
    ImageView ivSousuo;
    @BindView(R.id.et_title_sousuo)
    EditText etTitleSousuo;
    @BindView(R.id.rl_sousuo)
    RelativeLayout rlSousuo;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.iv_list_type)
    protected ImageView ivListType;
    private List<Fragment> fragmentList;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_base_tab_search;
    }

    @Override
    public void initView() {
    }

    @Override
    public void initData() {
        fragmentList = getFragmentList();
        initViewPager();
        initIndicator();
    }

    protected abstract List<Fragment> getFragmentList();

    private void initViewPager() {

        etTitleSousuo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                refresh(s.toString());
//                loadData(1, orderby);
            }
        });
        viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragmentList.get(i);
            }

            @Override
            public int getCount() {
                return fragmentList == null ? 0 : fragmentList.size();
            }

//            @Override
//            public int getItemPosition(@NonNull Object object) {
//                return PagerAdapter.POSITION_NONE;
//            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int position) {

            }
        });
    }

    protected abstract void refresh(String searchKey);

    /**
     * 添加指示器
     */
    private void initIndicator() {
        trackIndicator.setAdapter(viewPager, new IndicatorAdapter(this, new IndicatorAdapter.OnTabClickListener() {
            @Override
            public void tabIvClick(boolean selected) {
                selectChange(selected);
            }
        }, getItems()));
    }

    protected abstract void selectChange(boolean selected);

    protected abstract String[] getItems();

    @OnClick({R.id.iv_fanhui, R.id.iv_list_type})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_fanhui:
                KeyboardUtils.hideSoftInput(this);
                onBackPressed();
                break;
            case R.id.iv_list_type:
                ivListTypeClick();
                ivListType.setSelected(!ivListType.isSelected());
                break;
        }
    }

    protected abstract void ivListTypeClick();
}
